<?php
class Admin_ArticleController extends My_Controller_Admin_Action
{
	protected $articleModel = '';

	protected function _init()
	{
		$this->articleModel = new Admin_Model_Article();
	}

	public function indexAction()
	{
		$rowPerPage = $this->_getParam('rowperpage', 20);
		$currentPage = 1;
		$page = $this->_getParam('page', 1);
		if(!empty($page)) {
			$currentPage = $page;
		}

		$rows = $this->articleModel->getListArticle();
		$paginator = Zend_Paginator::factory($rows);
		$paginator->setItemCountPerPage($rowPerPage);
		$paginator->setCurrentPageNumber($currentPage);
		
		$this->view->page = $page;
		$this->view->paginator = $paginator;

	}

	public function addArticleAction()
	{
		$id = $this->_getParam('id');

		if(!$id)
		{
			$this->view->title = 'Ajouter une actualité';
		}else{
			$this->view->title = 'Edition';
			$actu = new Admin_Model_Article();
			$row = $actu->getArticle($id);
			$this->view->actu = $row;
		}
	}


	public function saveArticleAction()
	{
        $post = $this->getRequest()->getPost();
        //print_r($post);exit;
         
        $data = array(
            'titre'      => $post['titre'],
            'resume'     => addslashes($post['resume']),
            'contenu'    => addslashes($post['contenu']),
            'dtcreat'    => date('Y-m-d'),
            'soustitre'  => addslashes($post['soustitre']),
        	'num_temp'   => md5(uniqid(rand(), true))
        );
        
	    if($post['dtpubli']) {
	        $dtpubli = explode('/', $post['dtpubli']);
            $data['dtpubli'] = date("Y-m-d", mktime(0,0,0,$dtpubli[1], $dtpubli[0], $dtpubli[2]));
        }
        
///print_r($data);exit;
        $actuModel = new Admin_Model_Article();
        $num_actu = $actuModel->insert($data);

    	$upload_dir = APPLICATION_PATH . '/../public/fichiers/';
    	if ($_FILES['userfile']['tmp_name'] != "") {
    		$file = $_FILES['userfile'];
    		$test = preg_match('#\.(.+)$#', $file['name'], $matches);
    		$extension = $matches[1];		
    		$nom_pub = $num_actu .".".$extension;
    		//echo "fichier : " . $nom_pub."<br>";
    		if (move_uploaded_file($_FILES['userfile']['tmp_name'], $upload_dir.$nom_pub)) {
    		    $actuModel->update(array('fichier' => $nom_pub), 'p_actu='.$num_actu);
    		}	
    	}
    	if ($_FILES['userfile0']['tmp_name'] != "") {
    		$file = $_FILES['userfile0'];
    		$test = preg_match('#\.(.+)$#', $file['name'], $matches);
    		$extension = $matches[1];		
    		$nom_pub = $num_actu."_thumb.".$extension;
    		//echo "vignette : " . $nom_pub;
    		if (move_uploaded_file($_FILES['userfile0']['tmp_name'], $upload_dir.$nom_pub)) {
    		    $actuModel->update(array('vignette' => $nom_pub), 'p_actu='.$num_actu);
    		}	
    	}	
    	
    	$this->_redirect('admin/article');
	}
	
	public function updateArticleAction()
	{
    	$post = $this->getRequest()->getPost();
        //print_r($post);exit;

        $data = array(
            'titre'      => $post['titre'],
            'resume'     => addslashes($post['resume']),
            'contenu'    => addslashes($post['contenu']),
            'dtcreat'    => date('Y-m-d'),
            'soustitre'  => addslashes($post['soustitre'])
        );
        
	    if($post['dtpubli']) {
            $dtpubli = explode('/', $post['dtpubli']);
            $data['dtpubli'] = date("Y-m-d", mktime(0,0,0,$dtpubli[1], $dtpubli[0], $dtpubli[2]));
        }
        
        $num_actu  = $post['p_actu'];
        
        $actuModel = new Admin_Model_Article();
        $actuModel->update($data, 'p_actu = ' . $num_actu);
        
        $upload_dir = APPLICATION_PATH . '/../public/fichiers/';
    	if ($post[conserve] != "") {
    		if ($post[conserve] != 1) {
    			$fichier = $upload_dir . $post[conserve];
    			unlink($fichier);
    			$actuModel->update(array('fichier' => ''), 'p_actu=' . $num_actu);
    		}
    	}
    	// Insertion d'un nouveau fichier
    	if ($_FILES['userfile']['tmp_name'] != "") {
    		$file = $_FILES['userfile'];
    		$test = preg_match('#\.(.+)$#', $file['name'], $matches);
    		$extension = $matches[1];	
    		$nom_pub = $num_actu .".".$extension;
    		if (move_uploaded_file($_FILES['userfile']['tmp_name'], $upload_dir.$nom_pub)) {
    			if ($post[conserve] == 1) {
    			    $row = $actuModel->getFichier($num_actu);
    				$fichier = $upload_dir . $row['fichier'];
    				unlink($fichier);
    			}
    			
    			$actuModel->update(array('fichier' => $nom_pub), 'p_actu=' . $num_actu);
    		}	
    	}	
    	
    	// Si on ne veut pas conserver la vignette, on la delete
    	if ($post[conserveVI] != "") {
    		if ($post[conserveVI] != 1) {
    			$fichier = $upload_dir . $post[conserveVI];
    			unlink($fichier);
    			
    			$actuModel->update(array('vignette' => ''), 'p_actu=' . $num_actu);
    		}
    	}
    	// Insertion d'une nouvelle vignette
    	if ($_FILES['userfile0']['tmp_name'] != "") {
    		$file = $_FILES['userfile0'];
    		$test = preg_match('#\.(.+)$#', $file['name'], $matches);
    		$extension = $matches[1];	
    		$nom_pub = $num_actu .".".$extension;
    		if (move_uploaded_file($_FILES['userfile0']['tmp_name'], $upload_dir.$nom_pub)) {
    			if ($post[conserveVI] == 1) {
    			    
    			    $row = $actuModel->getVignette($num_actu);
    				$vignette = $upload_dir . $row['vignette'];
    				unlink($vignette);
    			}
    			
    			$actuModel->update(array('vignette' => $nom_pub), 'p_actu=' . $num_actu);
    		}	
    	}	
    
    	$this->_redirect('admin/article');
	}
	
	public function deleteArticleAction()
	{
		$id = $this->_getParam('id');
		$page = $this->_getParam('page', 1);
		$articleModel = new Admin_Model_Article();
		$articleModel->removeArticle($id);

		$param = '';
		if($page > 1) $param = 'index/page/' . $page;
		$this->_redirect('admin/article/' . $param);
		exit;
	}
}

